/**
 *	流程模块接口文件：file上传的文件管理
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Module.base.remind;



import com.framerock.baselib.Rock;
import com.framework.Flow.Flow;
import com.framework.Flow.FlowListener;

import java.util.ArrayList;
import java.util.Map;


public class FlowListenerremind extends FlowListener {


    public Boolean onModejs() { return true;}

    @Override
    public Boolean onRemind() {
        return false;
    }

    @Override
    public Boolean onisAdd() {
        return true;
    }

    @Override
    public Boolean onisEdit() {
        if(mFlow.isMydj())return true;
        if(mRequest.USERTYPE==2)return true;//平台管理员可编辑
        return super.onisEdit();
    }

    @Override
    public Boolean onisDelete() {
        if(mFlow.isMydj())return true;
        return super.onisDelete();
    }

    @Override
    public String onAction(String act, String params) {
        if(act.equals("clearrun"))mFlow.mModel.update("`lastdt`=NULL,`lastcont`=''","1=1");
        return super.onAction(act, params);
    }

    @Override
    public void onSaveafter(Map<String, String> da, String mid, Boolean isadd) {
        String mknum  = da.get("mknum");
        String mid1   = da.get("mid");

        //更新对应频率
        if(!Rock.isEmpt(mknum) && !Rock.isEmpt(mid1)){
            Flow flow = mFlow.let(mknum);
            Map<String, String> info = flow.remindInfo(),ds;
            String fields = Rock.getMapString(info, "fields");
            if(!Rock.isEmpt(fields)){
                ArrayList<Map<String, String>> rows = mFlow.mModel.getAll("`mknum`='"+mknum+"' AND `mid`="+mid1+" AND `status`=1", "*", "`optdt` DESC");
                rows = mFlow.replaceRowsList(rows);
                String ratecont = "";
                if(rows != null)for(int i=0; i<rows.size(); i++){
                    ds = rows.get(i);
                    if(i>0)ratecont+=";";
                    ratecont+=""+ds.get("rate_textcn")+ds.get("rateval")+"";
                }
                flow.mModel.update("`"+fields+"`='"+ratecont+"'", mid1);
            }
        }
    }

    @Override
    public void onDeleteafter() {
        this.onSaveafter(mFlow.data, mFlow.mid, false);
    }

    @Override
    public ArrayList<Map<String, String>> onFieldsArr(ArrayList<Map<String, String>> rows) {
        if(!mFlow.gtype.equals("showmknum"))return rows;
        ArrayList<Map<String, String>> data = Rock.getMapList();
        Map<String, String> da;
        String fields[] = new String[]{"title","mess","rate","rateval","startdt","status","lastdt"};
        for(int i=0; i<rows.size();i++){
            da = rows.get(i);
            if(Rock.inArray(fields, da.get("fields"))){
                data.add(da);
            }
        }
        return data;
    }
}
